<?php
/**
 * Created by PhpStorm.
 * User: Hk
 * Date: 2021/1/26
 * Time: 18:04
 */

namespace App\Http\Controllers\Estate;


use App\Http\Controllers\Controller;
use App\Models\Community;
use App\Models\Area;
use Illuminate\Http\Request;

class CommunityController extends Controller
{
    public $model;
    public function __construct(){
        $this->model = new Community();
    }

    /**
     * Notes: 小区列表
     * User: xiaohui
     * Date: 2021/3/17
     * Time: 14:12
     * email: 363876982@qq.com
     * @param Request $request
     */
    public function index(Request $request)
    {
        $user = $request->user();
        $param = $request->query();
        $where[] = ['is_deleted',0];
        if (isset($param['name']) && !empty($param['name'])) $where[] = ['name','like',"%{$param['name']}%"];
        if (isset($param['areas']) && !empty($param['areas'])) {
            [ $province , $city , $area ] = $param['areas'];
            $where[] = ['area','=',$area];
        }
        $lsit=$this->model->getPaginate( $where ,'id','desc', null, $request->get('limit', 20)) ;

        return $this->success($lsit );
    }

    /**
     * Notes: 获取省市区三级联动数据接口
     */
    public function area_lists(Area $area)
    {
        $lists = $area->with('children')->where('pid',0)->select('id as value','name as label','code','pid')->get()->toArray();
        return $this->success($lists);
    }
//    /**
//     * Notes: 删除状态
//     * User: xiaohui
//     * Date: 2021/3/17
//     * Time: 14:12
//     * email: 363876982@qq.com
//     * @param Request $request
//     */
//    public function del_status(Request $request)
//    {
//        $community_id = $request->post('id');
//        $community_ids = explode(",", $community_id);
//        return $this->model->updateByWherein('community_id', $community_ids, ['is_deleted' => 1]) ? $this->success('删除成功!') : $this->error('删除失败!');
//    }
//
//    /**
//     * Notes: 修改状态
//     * User: xiaohui
//     * Date: 2021/3/17
//     * Time: 14:12
//     * email: 363876982@qq.com
//     * @param Request $request
//     */
//    public function edit_status(Request $request)
//    {
//        $community_id = $request->post('community_id');
//        $status = $this->model->getValue(['community_id' => $community_id], 'status') == 1 ? 2 : 1;
//        return $this->model->updateByWhere(['community_id' => $community_id], ['status' => $status]) ? $this->success('修改成功!') : $this->error('修改失败!');
//    }

    /**
     * Notes: 新增小区
     */
    public function store(Request $request)
    {
        return $this->model->create($request->post()) ? $this->success('添加成功!') : $this->error('添加失败!');
    }

    /**
     * Notes: 单个小区详情
     */
    public function show($id)
    {
        return $this->success( $this->model->getInfo(['community_id' => $id]) );
    }

    /**
     * Notes: 修改小区
     */
    public function update(Request $request)
    {
        return $this->model->edit( $request->post() ) ? $this->success('修改成功!') : $this->error('修改失败!');
    }
    /**
     * Notes: 删除状态
     * User: xiaohui
     * Date: 2021/3/17
     * Time: 14:12
     * email: 363876982@qq.com
     * @param Request $request
     */
    public function del_status(Request $request)
    {
        $community_id = $request->post('id');
        $community_ids = explode(",",$community_id);
        return $this->model->updateByWherein('community_id' , $community_ids,['is_deleted' => 1]) ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    /**
     * Notes: 修改状态
     * User: xiaohui
     * Date: 2021/3/17
     * Time: 14:12
     * email: 363876982@qq.com
     * @param Request $request
     */
    public function edit_status(Request $request)
    {
        $community_id = $request->post('community_id');
        $status = $this->model->getValue(['community_id' => $community_id],'status') == 1 ? 2 : 1;
        return $this->model->updateByWhere(['community_id' => $community_id],['status' => $status]) ? $this->success('修改成功!') : $this->error('修改失败!');
    }
}
